﻿Public Class frmSocietes
    Private szSQL As String
    Private rs As ADODB.Recordset
    Dim OLD_CODE As String
    
    Private Sub frmSocietes_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        InitListeSocietes(0)
        ' utilisateur autorisé à modifier ?
        If gUserAcces < 9 Then
            BO_Enregistrer.Enabled = False
        Else
            BO_Enregistrer.Enabled = True
        End If

    End Sub

    Private Sub InitListeSocietes(ByVal ix As Integer)

        Dim nb As Integer = 0

        ' effacer la listview
        LV_SOCIETES.Items.Clear()

        ' Lire les SOCIETES
        szSQL = "CALL spGet_SOCIETES('')"
        GetRecordset(ctDSN_AEM, szSQL, rs)

        ' Load the data.
        Do While Not rs.EOF
            LV_SOCIETES.Items.Add(rs.Fields("code").Value)
            LV_SOCIETES.Items(nb).SubItems.Add(rs.Fields("nom").Value)
            LV_SOCIETES.Items(nb).SubItems.Add(rs.Fields("no_agrement").Value)
            ' Get the next record.
            rs.MoveNext()
            nb = nb + 1
        Loop
        ' Close the recordset AND connection.
        rs.Close()

        If nb > 0 Then
            LB_NbLignes.Text = CStr(nb) & " lignes"
        End If
        ' restaurer la position du curseur avant le init
        setIndice(ix, LV_SOCIETES)

    End Sub


    Private Sub LV_Societes_DoubleClick(ByVal sender As Object, ByVal e As System.EventArgs) Handles LV_SOCIETES.DoubleClick

        If LV_SOCIETES.SelectedItems.Count > 0 Then
            SocietetoScreen(LV_SOCIETES.SelectedItems(0).Text)
            ' afficher les détails de la formule
            TC_Details.Visible = True
        End If
        OLD_CODE = TE_CODE.Text

    End Sub

    Private Sub LV_Societes_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles LV_SOCIETES.KeyDown

        Select Case e.KeyCode
            ' touche Enter = DoubleClick
            Case Keys.Enter
                LV_Societes_DoubleClick(sender, e)

            Case Keys.Insert
                BO_ADD_LIGNE_Click(sender, e)

            Case Keys.Delete
                BO_DELETE_LIGNE_Click(sender, e)

        End Select

    End Sub

    Private Sub BO_Enregistrer_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BO_Enregistrer.Click

        ' mise à jour 
        szSQL = "CALL spUpd_SOCIETES('" & OLD_CODE & "','" & TE_CODE.Text & "','" & _
            nq(TE_NOM.Text) & "','" & TE_NO_AGREMENT.Text & "','" & TE_BNQ_NOM.Text & "','" & TE_BNQ_CPTE.Text & "','" & _
            TE_ADRESSE.Text & "','" & TE_ADRESSE2.Text & "','" & TE_TEL.Text & "','" & TE_JNL_VTE.Text & "'," & _
            TE_CPTE_TVA.Text & "," & TE_CPTE_BNQ.Text & "," & TE_CPTE_AUX.Text & ")"
        ExecQuery(ctDSN_AEM, szSQL)
        ' Cacher les détails
        BO_ANNULER_Click(sender, e)

    End Sub

    Private Sub SocietetoScreen(ByVal ref As String)

        ' Lire l'enreg sélectionné
        szSQL = "CALL spGet_SOCIETES('" & ref & "')"
        GetRecordset(ctDSN_AEM, szSQL, rs)
        If rs.RecordCount > 0 Then
            TE_CODE.Text = rs.Fields("code").Value
            TE_NOM.Text = rs.Fields("nom").Value
            TE_NO_AGREMENT.Text = rs.Fields("no_agrement").Value
            TE_BNQ_NOM.Text = nv(rs.Fields("bnq_nom").Value)
            TE_BNQ_CPTE.Text = nv(rs.Fields("bnq_cpte").Value)
            TE_ADRESSE.Text = nv(rs.Fields("adresse").Value)
            TE_ADRESSE2.Text = nv(rs.Fields("adresse2").Value)
            TE_CP_VILLE.Text = nv(rs.Fields("cp_ville").Value)
            TE_TEL.Text = nv(rs.Fields("tel").Value)
            TE_DATE_CPTA.Text = nv(rs.Fields("date_cpta").Value)
            TE_JNL_VTE.Text = nv(rs.Fields("jnl_vte").Value)
            TE_CPTE_TVA.Text = rs.Fields("cpte_tva").Value
            TE_CPTE_BNQ.Text = rs.Fields("cpte_bnq").Value
            TE_CPTE_AUX.Text = rs.Fields("cpte_aux").Value
        End If
        rs.Close()

    End Sub

    Private Sub BO_ANNULER_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BO_ANNULER.Click
        ' Cacher les détails
        TC_Details.Visible = False
        ' réaffiche la viewlist
        InitListeSocietes(LV_SOCIETES.SelectedItems(0).Index)
    End Sub

    Private Sub BO_DELETE_LIGNE_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BO_DELETE_LIGNE.Click
        ' utilisateur autorisé à supprimer ?
        If IsUserAuthorised(9) = False Then
            Exit Sub
        End If
        deleteEnregistrement("SOCIETES", LV_SOCIETES.SelectedItems(0).SubItems(1).Text, LV_SOCIETES.SelectedItems(0).Text)
        ' Cacher les détails
        BO_ANNULER_Click(sender, e)
    End Sub

    Private Sub BO_ADD_LIGNE_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BO_ADD_LIGNE.Click
        ' utilisateur autorisé à ajouter ?
        If IsUserAuthorised(9) = False Then
            Exit Sub
        End If
        ' Ajouter un nouveau élément
        szSQL = "spINS_SOCIETES('0')"
        If ExecQuery(ctDSN_AEM, szSQL) Then
            ' réaffiche la viewlist
            InitListeSocietes(0)
            ' ouvrir le fomulaire de modif
            LV_Societes_DoubleClick(sender, e)
        End If
    End Sub

End Class